package wyq.gy.service;

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;

import org.greenrobot.eventbus.EventBus;

import java.util.List;
import wyq.gy.Bean.robOrderMsg;
import wyq.gy.lib.Constants;


public class PollingService extends Service {
    public static final String ACTION = Constants.PACKNAMEPOLLING + ".PollingService";
    private static final String TAG = "PollingService";

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new PollingThread().start();
        return START_STICKY;
    }

    class PollingThread extends Thread {
        @Override
        public void run() {
            Log.d(TAG, "定时执行一次轮询");
            AVUser user = AVUser.getCurrentUser();
            if(user == null){
                return;
            }
               // user.getUsername();
            AVQuery<AVObject> avQuery = new AVQuery<>("RobMyOrderList");
            avQuery.whereEqualTo("EmployerID", user.getUsername());
            avQuery.whereEqualTo("State", "1");
            avQuery.findInBackground(new FindCallback<AVObject>() {
                @Override
                public void done(List<AVObject> lister, AVException e) {
                    Log.e(TAG, "找到数值");
                    if (lister != null && lister.size() != 0) {
                        for (AVObject avObject : lister) {
                            if (avObject.getString("State") != null) {
                                if (Constants.MyOrderObjectIdG == null) {
                                    Constants.MyOrderObjectIdG = avObject.getObjectId();
                                    robOrderMsg msg = new robOrderMsg();
                                    msg.robOrderName = avObject.getString("RoberName");
                                    msg.robWokerPhone = avObject.getString("robWokerPhone");
                                    msg.robBirth = avObject.getString("robBirth");
                                    msg.star = avObject.getString("star");
                                    msg.robWorkerId = avObject.getString("RoberId");
                                    msg.orderID = avObject.getString("OrderNumber");
                                    msg.robId = avObject.getObjectId();
                                    EventBus.getDefault().post(msg);
                                    Log.e(TAG, Constants.MyOrderObjectIdG);
                                }
                            }
                        }
                    }
                }
            });
        }
    }


}

